﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using OpenPop.Pop3;
using OpenPop.Mime;
using System.Data;
/// <summary>
/// Summary description for pops
/// </summary>
public class mypopclient : OpenPop.Pop3.Pop3Client
{
   
    public void Connect()
    {
        
        if (!this.Connected)
        {
            this.Connect("pop.gmail.com", 995, true);
            this.Authenticate("info.dawriyah@gmail.com", "dawryasystem");
        }
       
    }
    public DataTable GetAllMesseges()
    {
       DataTable dtMessages = new DataTable();


        this.Connect();
        int count = this.GetMessageCount();
        dtMessages.Columns.Add("MessageNumber");
        dtMessages.Columns.Add("From");
        dtMessages.Columns.Add("Subject");
        dtMessages.Columns.Add("DateSent");
        dtMessages.Columns.Add("Body");
        dtMessages.Columns.Add("Attachment");
        int counter = 0;
        for (int i = count; i >= 1; i--)
        {
            OpenPop.Mime.Message message = this.GetMessage(i);

            dtMessages.Rows.Add();
            dtMessages.Rows[dtMessages.Rows.Count - 1]["MessageNumber"] = i;
            dtMessages.Rows[dtMessages.Rows.Count - 1]["From"] = message.Headers.From;
            dtMessages.Rows[dtMessages.Rows.Count - 1]["Subject"] =message.Headers.Subject;
            dtMessages.Rows[dtMessages.Rows.Count - 1]["DateSent"] = message.Headers.DateSent;
            string vv = message.FindFirstPlainTextVersion().GetBodyAsText();
            vv = vv.Replace("<", "").Replace(">", "]");
            dtMessages.Rows[dtMessages.Rows.Count - 1]["Body"] = vv;// "hello"; //message.MessagePart == null ? "" : HttpUtility.HtmlEncode(message.FindFirstPlainTextVersion().GetBodyAsText());
            dtMessages.Rows[dtMessages.Rows.Count - 1]["Attachment"] = message.FindAllAttachments().Count;
            counter++;
        }

        return dtMessages;
    }
}
